package com.itzyf.pokemondata.activity.panel

import android.app.Activity
import android.content.Intent
import android.text.Html
import android.text.TextUtils
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbPokemon
import com.itzyf.pokemondata.bean.PokemonDo
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_panel_comparison.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2019/01/25 16:26
 */
class PanelComparisonActivity : BaseActivity() {
    private var pokemonDo1 = PokemonDo()
    private var pokemonDo2 = PokemonDo()

    override fun handlerActivity() {
        tvPokemon1.setOnClickListener {
            startActivityForResult(Intent(this, SelectPokemonActivity::class.java), 0x1)
        }
        tvPokemon2.setOnClickListener {
            startActivityForResult(Intent(this, SelectPokemonActivity::class.java), 0x2)
        }
        pokemonDo1.finisInit()
        pokemonDo2.finisInit()

        updatePanel()

        //红1
        cbRed1.setOnCheckedChangeListener { _, isChecked ->
            pokemonDo1.isRed1(isChecked)
            pokemonDo2.isRed1(isChecked)
            updatePanel()
        }
        //特训
        cbSpecialTraining.setOnCheckedChangeListener { _, isChecked ->
            val index = if (isChecked) 1 else 0
            pokemonDo1.setSpecialTrainingIndex(index)
            pokemonDo2.setSpecialTrainingIndex(index)
            updatePanel()
        }
        //亲密度
        cbIntimacy.setOnCheckedChangeListener { _, isChecked ->
            pokemonDo1.intimacy(isChecked)
            pokemonDo2.intimacy(isChecked)
            updatePanel()
        }
        //契约
        cbContract.setOnCheckedChangeListener { _, isChecked ->
            pokemonDo1.contract(if (isChecked) 800 else 0)
            pokemonDo2.contract(if (isChecked) 800 else 0)
            updatePanel()
        }

        val guildAttackValue = sp.getString("guildAttackValue", "0%")
        val guildDefenseValue = sp.getString("guildDefenseValue", "0%")
        tvGuildValue.text = "攻：$guildAttackValue；防：$guildDefenseValue"
        cbGuild.setOnCheckedChangeListener { _, isChecked ->

            val a = guildAttackValue!!.replace("%", "").toInt() * 0.01
            val b = guildDefenseValue!!.replace("%", "").toInt() * 0.01
            if (isChecked) {
                pokemonDo1.guild(a, b)
                pokemonDo2.guild(a, b)
            } else {
                pokemonDo1.guild(0.0, 0.0)
                pokemonDo2.guild(0.0, 0.0)
            }
            updatePanel()
        }

        //性格
        characterValue.text = "平衡"
        characterValue.setOnClickListener { showCharacterDialog() }


    }


    /**
     * 性格
     */
    private fun showCharacterDialog() {
        val character = arrayOf(
            "开朗（速度↑;特攻↓）",
            "胆小（速度↑;物攻↓）",
            "固执（物攻↑;特攻↓）",
            "保守（特攻↑;物攻↓）",

            "大胆（物防↑;物攻↓）",
            "淘气（物防↑;特攻↓）",
            "沉着（特防↑;物攻↓）",
            "慎重（特防↑;特攻↓）",

            "孤独（物攻↑;物防↓）",
            "调皮（物攻↑;特防↓）",
            "勇敢（物攻↑;速度↓）",

            "稳重（特攻↑;物防↓）",
            "马虎（特攻↑;特防↓）",
            "冷静（特攻↑;速度↓）",

            "急躁（速度↑;物防↓）",
            "天真（速度↑;特防↓）",

            "乐天（物防↑;特防↓）",
            "悠闲（物防↑;速度↓）",

            "温顺（特防↑;物防↓）",
            "狂妄（特防↑;速度↓）",

            "平衡"
        )

        val builder = QMUIDialog.CheckableDialogBuilder(this)
            .addItems(character) { dialog, which ->
                when (which) {
                    0 -> character(PokemonDo.SPEED, PokemonDo.SA)//开朗
                    1 -> character(PokemonDo.SPEED, PokemonDo.PA)//胆小
                    2 -> character(PokemonDo.PA, PokemonDo.SA)//固执
                    3 -> character(PokemonDo.SA, PokemonDo.PA)//保守

                    4 -> character(PokemonDo.PD, PokemonDo.PA)//大胆
                    5 -> character(PokemonDo.PD, PokemonDo.SA)//淘气
                    6 -> character(PokemonDo.SD, PokemonDo.PA)//沉着
                    7 -> character(PokemonDo.SD, PokemonDo.SA)//慎重

                    8 -> character(PokemonDo.PA, PokemonDo.PD)//孤独
                    9 -> character(PokemonDo.PA, PokemonDo.SD)//调皮
                    10 -> character(PokemonDo.PA, PokemonDo.SPEED)//勇敢

                    11 -> character(PokemonDo.SA, PokemonDo.PD)//稳重
                    12 -> character(PokemonDo.SA, PokemonDo.SD)//马虎
                    13 -> character(PokemonDo.SA, PokemonDo.SPEED)//冷静

                    14 -> character(PokemonDo.SPEED, PokemonDo.PD)//急躁
                    15 -> character(PokemonDo.SPEED, PokemonDo.SD)//天真

                    16 -> character(PokemonDo.PD, PokemonDo.SD)//乐天
                    17 -> character(PokemonDo.PD, PokemonDo.SPEED)//悠闲

                    18 -> character(PokemonDo.SD, PokemonDo.PD)//温顺
                    19 -> character(PokemonDo.SD, PokemonDo.SPEED)//狂妄

                    20 -> character(0, 0)//"平衡
                }
                characterValue.text =
                    character[which]
                dialog.dismiss()
            }
        builder.show()
    }

    private fun character(characterRise: Int, characterDecline: Int) {
        pokemonDo1.character(characterRise, characterDecline)
        pokemonDo2.character(characterRise, characterDecline)
        updatePanel()
    }


    override fun getContentViewId() = R.layout.activity_panel_comparison

    override fun getToolbarTitle() = "数据面板对比"

    /**
     * 更新面板数据
     */
    fun updatePanel() {
        progressHp.progress = pokemonDo1.hp.toFloat()
        progressPa.progress = pokemonDo1.pa.toFloat()
        progressPd.progress = pokemonDo1.pd.toFloat()
        progressSa.progress = pokemonDo1.sa.toFloat()
        progressSd.progress = pokemonDo1.sd.toFloat()
        progressSpeed.progress = pokemonDo1.speed.toFloat()


        progressHp2.progress = pokemonDo2.hp.toFloat()
        progressPa2.progress = pokemonDo2.pa.toFloat()
        progressPd2.progress = pokemonDo2.pd.toFloat()
        progressSa2.progress = pokemonDo2.sa.toFloat()
        progressSd2.progress = pokemonDo2.sd.toFloat()
        progressSpeed2.progress = pokemonDo2.speed.toFloat()


        tvHp.text = Html.fromHtml(result(progressHp2.progress - pokemonDo1.hp))
        tvPa.text = Html.fromHtml(result(progressPa2.progress - pokemonDo1.pa))
        tvPd.text = Html.fromHtml(result(progressPd2.progress - pokemonDo1.pd))
        tvSa.text = Html.fromHtml(result(progressSa2.progress - pokemonDo1.sa))
        tvSd.text = Html.fromHtml(result(progressSd2.progress - pokemonDo1.sd))
        tvSpeed.text = Html.fromHtml(result(progressSpeed2.progress - pokemonDo1.speed))

    }


    fun result(r: Float): String {
        return if (r >= 0) {
            "<font color=\"#f54545\">+${r.toInt()}</font>"
        } else {
            "<font color=\"#0f990f\">${r.toInt()}</font>"
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            data?.let {
                val objId = it.getStringExtra("objId")
                val dbPokemon = LitePal.where("objectId = ?", objId)
                    .findFirst(DbPokemon::class.java)
                val pokemonDo = PokemonDo()
                pokemonDo.hp = dbPokemon.hp
                pokemonDo.pa = dbPokemon.pa
                pokemonDo.pd = dbPokemon.pd
                pokemonDo.sa = dbPokemon.sa
                pokemonDo.sd = dbPokemon.sd
                pokemonDo.speed = dbPokemon.speed
                pokemonDo.red1 = dbPokemon.red1
                pokemonDo.specialTraining = dbPokemon.specialTraining
                if (requestCode == 0x1) {
                    pokemonDo1 = pokemonDo
                    pokemonDo1.finisInit()
                    pokemonDo1.setSpecialTrainingIndex(0)
                    tvPokemon1.text = dbPokemon.pokemonName
                } else {
                    pokemonDo2 = pokemonDo
                    pokemonDo2.finisInit()
                    pokemonDo2.setSpecialTrainingIndex(0)
                    tvPokemon2.text = dbPokemon.pokemonName
                }
                reset()
            }
        }

    }

    private fun reset() {
        cbSpecialTraining.isEnabled =
            !(TextUtils.isEmpty(pokemonDo1.specialTraining) || TextUtils.isEmpty(pokemonDo2.specialTraining))
        cbRed1.isEnabled =
            !(TextUtils.isEmpty(pokemonDo1.red1) || TextUtils.isEmpty(pokemonDo2.red1))

        cbRed1.isChecked = false
        cbSpecialTraining.isChecked = false
        cbIntimacy.isChecked = false
        cbContract.isChecked = false
        cbGuild.isChecked = false

        updatePanel()
    }
}